<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  barData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let option = {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
        { offset: 0, color: '#2bd3d7' },
        { offset: 1, color: '#1d649c' },
      ]),
      barMaxWidth: pxToRem(11 * props?.dialogData?.fontSizeTimes),
      grid: {
        left: '3%',
        right: '5%',
        bottom: 0,
        top: '15%',
        containLabel: true
      },
      dataZoom: { // 数据过多内部拖动逻辑
        type: "inside",
        zoomLock: true, // 不可缩放
        startValue: 0,
        endValue: props?.dialogData?.fontSizeTimes === 1 ? 10 : 5,
      },
      xAxis: {
        type: 'category',
        data: props?.barData?.xAxisData,
        boundaryGap: true,
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
          lineStyle: {
            color: '#28FCFF'
          }
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          color: 'white',
          fontSize: pxToRem(14 * props?.dialogData?.fontSizeTimes),
          interval: 0,
        }
      },
      yAxis: {
        name: '单位：万套',
        nameGap: pxToRem(15 * props?.dialogData?.fontSizeTimes),
        nameTextStyle: {
          color: "white",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          fontWeight: 'lighter',
          padding: [0, 0, 0, pxToRem(-15 * props?.dialogData?.fontSizeTimes)]
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: 'rgba(49,103,156,0.6)',
            type: 'dashed'
          }
        },
        axisLabel: {
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        }
      },
      series: [
        {
          data: props?.barData?.seriesData,
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: '#024065',
          }
        }
      ],
    }
    option && myChart.value.setOption(option, true);
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h250 {
  height: 250px;
}
.dialog-h250 {
  width: 1120px;
  height: 500px;
}
</style>